home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / xlib / image.c.z / image.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.5 KB  |  136 lines

  1. #include "gl/image.h"
  2. #include "GL/glx.h"
  3. #include <X11/keysym.h>
  4. #include "stdlib.h"
  5.  
  6. short rbuf[8192]; 
  7. short gbuf[8192]; 
  8. short bbuf[8192]; 
  9.  
  10. Display *dpy;
  11. Window window;
  12.  
  13. static void make_window(int width, int height, char *name, int border);
  14.  
  15. main(int argc,char *argv[]) {
  16.     register IMAGE *image;
  17.     register int x, y, xsize, ysize;
  18.     register int z, zsize;
  19.     unsigned char *buf;
  20.     int border = 1;
  21.  
  22.     if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'n') {
  23.     border = 0;
  24.     argc--; argv++;
  25.     }
  26.     if( argc<2 ) {
  27.     fprintf(stderr,"usage: image [-n] inimage.rgb\n");
  28.     exit(1);
  29.     } 
  30.     if( (image=iopen(argv[1],"r")) == NULL ) {
  31.     fprintf(stderr,"image: can't open input file %s\n",argv[1]);
  32.     exit(1);
  33.     }
  34.     xsize = image->xsize;
  35.     ysize = image->ysize;
  36.     zsize = image->zsize;
  37.     if(zsize<3) {
  38.     fprintf(stderr,"image: this is not an RGB image file\n");
  39.     exit(1);
  40.     }
  41.     buf = (unsigned char *)malloc(xsize*ysize*3);
  42.     for(y=0; y<ysize; y++) {
  43.     getrow(image,rbuf,y,0);
  44.     getrow(image,gbuf,y,1);
  45.     getrow(image,bbuf,y,2);
  46.     for(x = 0; x < xsize; x++) {
  47.         buf[(y*xsize+x)*3+0] = rbuf[x];
  48.         buf[(y*xsize+x)*3+1] = gbuf[x];
  49.         buf[(y*xsize+x)*3+2] = bbuf[x];
  50.     }
  51.     
  52.     }
  53.     make_window(xsize, ysize, argv[1], border);
  54.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  55.     glMatrixMode(GL_PROJECTION);
  56.     glOrtho(0, image->xsize, 0, image->ysize, -1, 1);
  57.     glMatrixMode(GL_MODELVIEW);
  58.     glRasterPos2i(0, 0);
  59.     while(1) {
  60.     XEvent ev;
  61.     XNextEvent(dpy, &ev);
  62.     switch(ev.type) {
  63.     case Expose:
  64.         glClearColor(0.5, 0.5, 0.5, 0.5);
  65.         glClear(GL_COLOR_BUFFER_BIT);
  66.         glDrawPixels(xsize, ysize, GL_RGB, GL_UNSIGNED_BYTE, buf);
  67.         break;
  68.     case KeyPress: {
  69.         char buf[100];
  70.         int rv;
  71.         KeySym ks;
  72.  
  73.         rv = XLookupString(&ev.xkey, buf, sizeof(buf), &ks, 0);
  74.         switch (ks) {
  75.         case XK_Escape:
  76.         exit(0);
  77.         }
  78.         break;
  79.     }
  80.     }
  81.     }
  82. }
  83.  
  84. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, None };
  85.  
  86. void
  87. noborder(Display *dpy, Window win) {
  88.     struct { long flags,functions,decorations,input_mode; } *hints;
  89.     int fmt;
  90.     unsigned long nitems,byaf;
  91.     Atom type,mwmhints = XInternAtom(dpy,"_MOTIF_WM_HINTS",False);
  92.  
  93.     XGetWindowProperty(dpy,win,mwmhints,0,4,False, mwmhints,&type,&fmt,&nitems,&byaf,(unsigned char**)&hints);
  94.  
  95.     if (!hints) hints = (void *)malloc(sizeof *hints);
  96.     hints->decorations = 0;
  97.     hints->flags |= 2;
  98.  
  99.     XChangeProperty(dpy,win,mwmhints,mwmhints,32,PropModeReplace,(unsigned char *)hints,4);
  100.     XFlush(dpy);
  101.     free(hints);
  102. }
  103.  
  104. static void
  105. make_window(int width, int height, char *name, int border) {
  106.     XVisualInfo *vi;
  107.     Colormap cmap;
  108.     XSetWindowAttributes swa;
  109.     GLXContext cx;
  110.     XSizeHints sizehints;
  111.  
  112.     dpy = XOpenDisplay(0);
  113.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  114.     printf("can't find requested visual\n");
  115.     exit(1);
  116.     }
  117.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  118.  
  119.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  120.                            vi->visual, AllocNone);
  121.     sizehints.flags = 0;
  122.  
  123.     swa.border_pixel = 0;
  124.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  125.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height,
  126.                         0, vi->depth, InputOutput, vi->visual,
  127.                         CWBorderPixel|CWColormap|CWEventMask, &swa);
  128.     XMapWindow(dpy, window);
  129.     XSetStandardProperties(dpy, window, name, name,
  130.         None, (void *)0, 0, &sizehints);
  131.  
  132.     if (!border) noborder(dpy, window);
  133.  
  134.     glXMakeCurrent(dpy, window, cx);
  135. }
  136.